package com.tiancikeji.zaoke.app.ajax.token;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tiancikeji.zaoke.app.common.dispatch.AjaxClass;
import com.tiancikeji.zaoke.app.common.dispatch.AjaxMethod;
import com.tiancikeji.zaoke.app.constants.ErrorCodeConstants;
import com.tiancikeji.zaoke.app.model.token.Token;
import com.tiancikeji.zaoke.app.service.token.TokenService;
import com.tiancikeji.zaoke.app.util.ZaokeUtils;

@AjaxClass
public class TokenAjax {
	
	private final Log logger = LogFactory.getLog(getClass());
	
	private TokenService tokenService;

	@AjaxMethod(path = "onstart")
	public Map<Object, Object> onStart(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		
		logger.debug("Entering onStart <" + params + ">");
		if(params != null && params.containsKey("userid") && params.containsKey("token")){
			String tmp = params.get("token");
			long userid = ZaokeUtils.getLong(params.get("userid"));
			
			if(userid > 0 && StringUtils.isNotBlank(tmp)){
				Token token = new Token();
				token.setUserid(userid);
				token.setToken(tmp);
				tokenService.insert(token);
				
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			}
		}
			
		return result;
	}

	public TokenService getTokenService() {
		return tokenService;
	}

	public void setTokenService(TokenService tokenService) {
		this.tokenService = tokenService;
	}

}
